package com.fsh.anime.controller;

import com.fsh.anime.model.dtos.ArticleDto;
import com.fsh.anime.model.dtos.PageArticleDto;
import com.fsh.anime.model.vos.Result;
import com.fsh.anime.service.ArticleService;
import com.fsh.anime.service.DictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/article")
@Slf4j
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private DictionaryService dictionaryService;

    /**
     * @param dto
     * @param results
     * @return {@link Result}
     * 发布文章
     */
    @PostMapping("/addArticle")
    public Result addArticle(@RequestBody @Validated ArticleDto dto, BindingResult results){
        if (results.hasErrors()){
            return Result.error(results.getFieldError().getDefaultMessage());
        }
//        log.info("文章Dto,{}",dto);
        return articleService.addArticle(dto);
    }

    /**
     * @param type
     * @return {@link Result}
     * 获取专栏分类
     */
    @GetMapping("/getTypeList/{type}")
    public Result getTypeList(@PathVariable Integer type){
        return dictionaryService.getTypeList(type);
    }

    /**
     * @param dto
     * @param results
     * @return {@link Result}
     * 根据分页参数获取文章列表。特别注意，获取当前登录用户的文章。
     */
    @PostMapping("/getMyArticleList")
    public Result getMyArticleList(@RequestBody @Validated PageArticleDto dto,BindingResult results){
        if (results.hasErrors()){
            return Result.error(results.getFieldError().getDefaultMessage());
        }
//        log.info("查询文章分页参数,{}",dto);
        return articleService.getMyArticleList(dto);
//        return null;
    }

    /**
     * @param dto
     * @param results
     * @return {@link Result}
     * 根据分页参数获取广场文章列表。
     */
    @PostMapping("/getSquareArticleList")
    public Result getSquareArticleList(@RequestBody @Validated PageArticleDto dto, BindingResult results){
        if (results.hasErrors()){
            return Result.error(results.getFieldError().getDefaultMessage());
        }
//        log.info("查询文章分页参数,{}",dto);
        return articleService.getSquareArticleList(dto);
    }

    /**
     * 获取一篇文章
     *
     * @param id 编号
     * @return {@link Result}
     */
    @GetMapping("/getOneArticle")
    public Result getOneArticle( Long id){
//        log.info("{}",id);
//        return null;
        return articleService.getOneArticle(id);
    }

    /**
     * @param file
     * @return {@link String}
     * 上传文章 封面 的图片
     */
    @PostMapping("/uploadImg")
    public String uploadImg(MultipartFile file){
        return articleService.uploadImg(file);
    }

    /**
     * 文章点赞
     *
     * @param articleId 文章编号
     * @param likesUp   点赞标识
     * @return {@link Result}
     */
    @PostMapping("/likeArticle/{articleId}/{likesUp}")
    public Result likeArticle(@PathVariable("articleId") String articleId,@PathVariable("likesUp") Integer likesUp){
//        log.info("{},{}",articleId,likesUp);
        return articleService.likeArticle(articleId,likesUp);
    }

    /**
     * 收藏文章
     *
     * @param articleId 文章编号
     * @param collectUp 收藏行为
     * @return {@link Result}
     */
    @PostMapping("/collectArticle/{articleId}/{collectUp}")
    public Result collectArticle(@PathVariable("articleId") Long articleId,@PathVariable("collectUp") Integer collectUp){
//        log.info("{},{}",articleId,collectUp);
        return articleService.collectArticle(articleId,collectUp);
    }

}
